package com.github.aloxc.plugin.restplus.utils;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.popup.PopupBalloon;
import com.intellij.ide.script.IdeScriptEngine;
import com.intellij.ide.script.IdeScriptEngineManager;
import com.intellij.json.JsonUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import org.apache.commons.io.FileUtils;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
 *
 * @author leero
 */
public class JavaScriptUtil {
    public static String format(Project project,String content, int tabSize, char tabChar){
        InputStream inputStream = null;
        InputStreamReader reader = null;
//        String s = "client.test(\"响应状态码为200\",function() {    client.assert(response.status === 200, \"响应状态码不是200\");});client.test(\"响应文档类型是json\",function() {      var type = response.contentType.mimeType;    client.assert(type === \"application/json\", \"响应文档类型不是json，而是 '\" + type + \"'\");});client.test(\"响应数据为 aaa \",function() {    client.log(\"开始测试\");    client.assert(response.body.data === \"aaa\", \"响应数据不是 aaa\");});client.test(\"响应内容中users的length为2\",function() { client.assert(response.body.users.length === 2, \"响应内容中users的length不是2 \"); });";
        try {
            String formatFile = Registry.stringValue(Consts.FORMAT_REGISTER);
            ScriptEngineManager scriptEngineManager = new ScriptEngineManager(null);
            ScriptEngine engine = scriptEngineManager.getEngineByName("nashorn");
            inputStream = JavaScriptUtil.class.getResourceAsStream(formatFile);
            reader = new InputStreamReader((InputStream) inputStream, StandardCharsets.UTF_8);
            char[] b = new char[4096];
            int len;
            StringBuffer buf = new StringBuffer();
            while ((len = reader.read(b)) != -1) {
                String out = new String(b, 0, len);
                buf.append(StringUtil.replaceUnicodeEscapeSequences(out));
            }
            String format = buf.toString();
            engine.eval(format);
            String result = (String) ((Invocable) engine).invokeFunction("js_beautify", content, 2, ' ');
            return result;
        }catch (Exception e){
            Writer result = new StringWriter();
            PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            String msg = result.toString();
            msg.replaceAll("\\\\r\\\\n","<br>");
            Log.log1("格式化javascript代码异常" + JavaScriptUtil.class.getName() +"-" ,content);
            PopupBalloon.showNotification(project,"格式化javascript代码异常<br>" + msg,false);
        }finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            }catch (Exception e){
            }
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            }catch (Exception e){
            }
        }
        return null;
    }


}
